import Config from "@/common/config.js"
import * as Common from '@/common/common.js'
import * as db from '@/common/db.js'


class Api {
	constructor(arg) {

	}

	static request(url = '', data = {}, type = 'POST', isShow = true, header = {}) {
		if (isShow) {
			uni.showLoading({
				title: "加载中",
				mask: true,
			});
		}

		header.token = db.get('auth');
		var share = uni.getStorageSync('share');
		if (share) {
			header.shareUid = share.share_uid;
			header.shareGid = share.share_gid;
		}
		return new Promise((resolve, reject) => {
			uni.request({
				method: type,
				url: Config.baseUrl + url,
				data: data,
				header: header,
				dataType: 'json',
				success: (res) => {
				
					if(res.data.code == 5){
						Common.errorToShow(res.msg, () => {
							//清楚缓存
						    uni.clearStorage()
							db.clear()
								
							uni.navigateTo({
								url: '/pages/login/login'
							})
						})
						return
					}
					
					
					// 如果返回错误码就退出登录去登录页,没有就返回
					if (res.data.hasOwnProperty('errcode')) {
						var code = res.data.errcode.toString()
						var errcode = code.charAt(0)
						if (errcode == 4) {
							uni.clearStorage()
							Common.errorToShow(res.msg, () => {
								uni.clearStorage()
								db.clear()
									
									
								uni.navigateTo({
									url: '/pages/login/login'
								})
							})
						}
					} else {
						
				
						
						// 如果接口返回的用户信息和token就设置到缓存
						if (res.data.hasOwnProperty('token')) {
							db.set('auth', res.data.token)
						}
						
						if (res.data.hasOwnProperty('user')) {
							if (res.data.user && (!db.get('userInfo') || db.get('userInfo')
									.membe_id === res.data.user.membe_id)) {
								
								//
								if (res.data.hasOwnProperty('token')) {
									db.set('userInfo', res.data.user)
								}
								
								if (!res.data.user.openid) {
								// 	wx.login({
								// 		success: res => {
								
								// 			if (res.code) {
								// 				this.request('user/postWxCode', {code:res.code}, 'POST', false)
								// 			}
								// 		}
								// 	})
								}
							}
							// Common.loginIm(res.data.user)
						}
						if (!res.data.code) {
							Common.errorToShow(res.data.msg)
						}
						resolve(res.data)
					}
				},
				fail: (err) => {
					reject(err)
				},
				complete: () => {
					uni.hideLoading();
				}
			})
		});

	}

	static get(url = '', data = {}, type = "GET", header = {}) {
		return this.request(url, data, 'GET', header)
	}

	// 图片上传
	static uploadImg(method, data = {}, callback, num = 1, type, header = {}) {
		if (type) {
			method = type + '/' + method
		} else {
			method = method
		}

		header.token = db.get('auth');

		uni.uploadFile({
			url: Config.baseUrl + method,
			filePath: data,
			fileType: 'image',
			name: 'file',
			header: header,
			formData: data,
			success: (uploadFileRes) => {
				callback(JSON.parse(uploadFileRes.data))
			},
			fail: (error) => {
				if (error && error.response) {
					
					uni.showToast({
						title: '上传错误',
						icon: 'none',
						duration: 1000
					});
				}
			},
			complete: () => {
				setTimeout(function() {
					uni.hideLoading();
				}, 600);
			},
		});
	}

	// 选择图片上传
	static chooseImage(method, data = {}, callback, num = 9, type, header = {}) {
		if (type) {
			method = type + '/' + method
		} else {
			method = method
		}

		header.token = db.get('auth');
		uni.chooseImage({
			count: num,
			success: (res) => {
				uni.showLoading({
					title: '上传中...'
				});
				let tempFilePaths = res.tempFilePaths
				for (var i = 0; i < tempFilePaths.length; i++) {
					data.file = tempFilePaths[i]
					uni.uploadFile({
						url: Config.baseUrl + method,
						filePath: tempFilePaths[i],
						fileType: 'image',
						name: 'file',
						header: header,
						formData: data,
						success: (uploadFileRes) => {
							callback(JSON.parse(uploadFileRes.data))
						},
						fail: (error) => {
							if (error && error.response) {
						
								uni.showToast({
									title: '上传错误',
									icon: 'none',
									duration: 1000
								});
							}
						},
						complete: () => {
							setTimeout(function() {
								uni.hideLoading();
							}, 600);
						},
					});
				}
			}
		});
	}
}
export default Api